/**
 * 股票URL生成工具模块
 */

// 基础URL配置
const BASE_URLS = {
  EASTMONEY: 'https://datacenter-web.eastmoney.com/api/data/v1/get',
  IWENCAI: 'https://www.iwencai.com/diag/block-detail',
  PUSH2: 'https://push2.eastmoney.com/api/qt/stock',
  PUSH2HIS: 'https://push2his.eastmoney.com/api/qt/stock'
};

/**
 * 获取股票市场类型
 * @param {string} code 股票代码
 * @returns {number} 0: 深市, 1: 沪市
 */
const getStockMarket = (code) => {
  if (code.startsWith('002') || code.startsWith('300') || code.startsWith('000')) {
    return 0;
  }
  return 1;
};

/**
 * 生成K线数据URL
 * @param {string} code 股票代码
 * @param {number} type 数据类型(1:天数, 2:周数, 3:月数)
 * @param {number} limit 数据条数
 * @returns {string} URL
 */
export const getKlineUrl = (code, type, limit = 362) => {
  const market = getStockMarket(code);
  const klt = type === 1 ? 101 : type === 2 ? 102 : 103;
  
  return `${BASE_URLS.PUSH2HIS}/kline/get?secid=${market}.${code}&fields1=f1,f2,f3,f4,f5&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&lmt=${limit}&klt=${klt}&fqt=1&end=30000101`;
};

/**
 * 生成实时资金流向URL
 * @param {string} code 股票代码
 * @returns {string} URL
 */
export const getMoneyFlowUrl = (code) => {
  const market = getStockMarket(code);
  return `${BASE_URLS.PUSH2}/fflow/kline/get?lmt=0&klt=1&fields1=f1,f2,f3,f7&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64,f65&secid=${market}.${code}`;
};

/**
 * 生成历史资金流向URL
 * @param {string} code 股票代码
 * @param {number} klt K线类型
 * @param {boolean} isRealTime 是否实时数据
 * @returns {string} URL
 */
export const getHistoryMoneyFlowUrl = (code, klt, isRealTime = false) => {
  const market = getStockMarket(code);
  
  if (isRealTime) {
    return `${BASE_URLS.PUSH2}/ulist.np/get?fltt=2&secids=${market}.${code}&fields=f61,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87,f64,f65,f70,f71,f76,f77,f82,f83,f164,f166,f168,f170,f172,f252,f253,f254,f255,f256,f124,f6,f278,f279,f280,f281,f282`;
  }
  
  return `${BASE_URLS.PUSH2HIS}/fflow/daykline/get?lmt=0&klt=${klt}&fields1=f1,f2,f3,f7&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64,f65&secid=${market}.${code}`;
};

/**
 * 生成上证指数URL
 * @returns {string} URL
 */
export const getShanghaiIndexUrl = () => {
  return `${BASE_URLS.PUSH2HIS}/kline/get?secid=1.000001&fields1=f1,f2,f3,f4,f5&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&lmt=100&klt=101&fqt=1&end=30000101`;
};

/**
 * 生成股票评论URL
 * @param {string} code 股票代码
 * @returns {string} URL
 */
export const getStockCommentUrl = (code) => {
  return `https://data.eastmoney.com/stockcomment/api/${code}.json`;
};

/**
 * 生成股票详情URL
 * @param {string} code 股票代码
 * @returns {string} URL
 */
export const getStockDetailUrl = (code) => {
  return `https://data.eastmoney.com/stockcomment/stock/${code}.html#zlkp`;
};

/**
 * 生成股票搜索URL
 * @param {string} keyword 搜索关键词
 * @returns {string} URL
 */
export const getStockSearchUrl = (keyword) => {
  return `https://www.iwencai.com/stockpick/search?typed=1&preParams=&ts=1&f=1&qs=index_rewrite&selfsectsn=&querytype=stock&searchfilter=&tid=stockpick&w=${keyword}`;
}; 